<template>
	<!-- 商品列表 横向 -->
	<view class="shopSidewise-main" @click="gotoShopInfo(shopInfo.id)">
		<view class="shopSidewise-image">
			<image :src="imageUrls(shopInfo.fileUrl)"class="shopImage"></image>
		</view>
		<view class="shopSidewise-info">
			<view class="shopSidewise-info-name">
				{{ shopInfo.name }}
			</view>
			<view class="shopSidewise-info-des">规格：{{ shopInfo.spec }}</view>
			<view class="shopSidewise-info-tags">
				<uni-tag :text="shopInfo.artid ? '云仓发货' : '商家自营'" type="error" size="mini" :inverted="true" style="margin-right: 10rpx" />
				<uni-tag :text="shopInfo.recipeType ? '处方药' : '非处方药'" type="error" size="mini" />
			</view>
			<view class="shopSidewise-info-pri">
				￥
				<text style="font-size: 32rpx">{{ getShopPrice(shopInfo)}}</text>
			</view>
			<view class="shopSidewise-info-salesVolume">
				<view class="shop-salesvolume">
					剩余
					<text style="padding: 0px 6rpx">{{ shopInfo.total }}</text>
					件
				</view>
				<view class="shop-cart" @click.stop="addCard(shopInfo.id)">
					<u-icon name="shopping-cart" size="18" color="#fff"></u-icon>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import { cloudAddCart } from '../../api/home/index.js';
import {imageUrl} from "../../utils/tools.js"
export default {
	name: 'hwxcShopSidewise',
	data() {
		return {
			ratio:0
		};
	},
	props: {
		// 商品数据
		shopInfo: {
			type: Object,
			default: () => {}
		},
		shopType: {
			type: String,
			default: ''
		}
	},
	mounted() {
		this.ratio = this.$store.state.commInitInfo.yyd_ratio;
	},
	methods: {
		gotoShopInfo(id) {
			uni.navigateTo({
				url: `/pages/shoppingInfo/shoppingInfo?id=${id}`
			});
		},

		// 图片路径拼接
		imageUrls(fileUrl) {
			return imageUrl(fileUrl)
		},

		// 加入购物车
		async addCard(id) {
			await this.getcloudAddCart(id);
		},

		// 全国送 - 加入购物车
		async getcloudAddCart(id) {
			try {
				const queryData = {
					id,
					num: 1
				};
				const resData = await cloudAddCart(queryData);
				if (resData.code == 1) {
					uni.showToast({
						title: '添加成功',
						icon: 'none'
					});
				} else {
					uni.showToast({
						title: '添加失败',
						icon: 'error'
					});
				}
			} catch (e) {
				throw Error(e);
			}
		},
		
		getShopPrice(item) {
			// 云店根据费率计算
			if (item.type == 0) {
				if(!item.store_id){
					let pic = item.price != '0.00' ? item.price : item.saleprice;
					return (Number(pic) + (Number(pic) * this.ratio) / 100).toFixed(2);
				}else{
					return item.price != '0.00' ? item.price : item.saleprice;
				}
			} else {
				return item.price != '0.00' ? item.price : item.saleprice;
			}
		}
	}
};
</script>

<style scoped lang="scss">
.shopSidewise-main {
	display: flex;
	.shopSidewise-image {
		width: 240rpx;
		.shopImage {
			width: 220rpx;
			height: 220rpx;
			background-color: #f3f3f3;
		}
	}

	.shopSidewise-info {
		flex: 1;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		.shopSidewise-info-name {
			font-size: 27rpx;
			font-weight: bold;
			display: -webkit-box;
			-webkit-line-clamp: 2;
			-webkit-box-orient: vertical;
			overflow: hidden;
		}

		.shopSidewise-info-des {
			color: #999;
			font-size: 26rpx;
			margin-top: 8rpx;
		}

		.shopSidewise-info-tags {
			margin-bottom: 8rpx;
			margin: 10rpx 0px;
			display: flex;
			width: 100%;
		}

		.shopSidewise-info-pri {
			font-size: 24rpx;
			color: red;
			font-weight: bold;
		}

		.shopSidewise-info-salesVolume {
			display: flex;
			align-items: center;
			justify-content: space-between;

			.shop-salesvolume {
				color: #999;
				font-size: 24rpx;
			}

			.shop-cart {
				height: 45rpx;
				width: 45rpx;
				border-radius: 50%;
				background-color: red;
				display: flex;
				justify-content: center;
				align-items: center;
			}
		}
	}
}
</style>
